﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GMusicBox.Core;
using System.Runtime.InteropServices;

namespace GMusicBox
{
    public partial class frmMini : Form
    {
        public frmMini(frmMain fm)
        {
            InitializeComponent();
            this.fm = fm;
        }

        frmMain fm;
        bool closeApp = false;

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == (int)WM.WINDOWPOSCHANGING)
            {
                WINDOWPOS wp = (WINDOWPOS)m.GetLParam(typeof(WINDOWPOS));
                Rectangle sBound = Screen.PrimaryScreen.WorkingArea;
                if (wp.y - 10 <= sBound.Top) wp.y = sBound.Top;
                if (wp.x - 10 <= sBound.Left) wp.x = sBound.Left;
                if (wp.y + wp.cy + 10 >= sBound.Bottom) wp.y = sBound.Bottom - wp.cy;
                if (wp.x + wp.cx + 10 >= sBound.Right) wp.x = sBound.Right - wp.cx;
                Marshal.StructureToPtr(wp, m.LParam, false);
            }
            base.WndProc(ref m);
        }

        private void frmMini_Load(object sender, EventArgs e)
        {
            this.Size = new Size(300, 28);
        }

        private void frmMini_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Capture = false;
                Message msg = Message.Create(Handle, (int)WM.NCLBUTTONDOWN, (IntPtr)HT.CAPTION, IntPtr.Zero);
                WndProc(ref msg);
            }
            else if (e.Button == MouseButtons.Right)
                fm.mnuNotify.Show(this, e.Location);
        }

        private void btnList_Click(object sender, EventArgs e)
        {
            ContextMenu cm = new ContextMenu();

            cm.MenuItems.Add("我的列表");
            for (int i = 0; i < fm.songs[0].Count; i++)
            {
                cm.MenuItems[0].MenuItems.Add(fm.songs[0][i].Name);
                for (int j = 0; j < fm.songs[0][i].Songs.Count; j++)
                {
                    MenuItem m = new MenuItem(fm.songs[0][i][j].ToString());
                    m.Tag = new int[] { 0, i, j };
                    if (fm.be.PlayState == BassPlayState.Playing &&
                        fm.playing[0] == 0 && fm.playing[1] == i && fm.playing[2] == j) m.Checked = true;
                    m.Click += new EventHandler(m_Click);
                    cm.MenuItems[0].MenuItems[cm.MenuItems[0].MenuItems.Count - 1].MenuItems.Add(m);
                }
            }

            cm.MenuItems.Add("音乐榜单");
            for (int i = 0; i < fm.songs[1].Count; i++)
            {
                cm.MenuItems[1].MenuItems.Add(fm.songs[1][i].Name);
                for (int j = 0; j < fm.songs[1][i].Songs.Count; j++)
                {
                    MenuItem m = new MenuItem(fm.songs[1][i][j].ToString());
                    m.Tag = new int[] { 1, i, j };
                    if (fm.be.PlayState == BassPlayState.Playing &&
                        fm.playing[0] == 1 && fm.playing[1] == i && fm.playing[2] == j) m.Checked = true;
                    m.Click += new EventHandler(m_Click);
                    cm.MenuItems[1].MenuItems[cm.MenuItems[1].MenuItems.Count - 1].MenuItems.Add(m);
                }
            }

            cm.MenuItems.Add("专题列表");
            for (int i = 0; i < fm.songs[2].Count; i++)
            {
                cm.MenuItems[2].MenuItems.Add(fm.songs[2][i].Name);
                for (int j = 0; j < fm.songs[2][i].Songs.Count; j++)
                {
                    MenuItem m = new MenuItem(fm.songs[2][i][j].ToString());
                    m.Tag = new int[] { 2, i, j };
                    if (fm.be.PlayState == BassPlayState.Playing &&
                        fm.playing[0] == 2 && fm.playing[1] == i && fm.playing[2] == j) m.Checked = true;
                    m.Click += new EventHandler(m_Click);
                    cm.MenuItems[2].MenuItems[cm.MenuItems[2].MenuItems.Count - 1].MenuItems.Add(m);
                }
            }
            cm.Show(btnList, new Point(0, btnList.Height));
        }

        private void m_Click(object sender, EventArgs e)
        {
            MenuItem mi = ((MenuItem)sender);
            int[] data = (int[])mi.Tag;
            fm.SetPlaying(data[0], data[1], data[2]);
        }

        private void frmMini_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (closeApp)
            {
                fm.Close();
                if (!fm.closed)
                {
                    closeApp = false;
                    e.Cancel = true;
                }
            }
            else
            {
                e.Cancel = true;
                this.Hide();
            }
        }

        private void btnReturnMain_Click(object sender, EventArgs e)
        {
            closeApp = false;
            this.Close();
        }

        private void frmMini_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F4 && e.Alt == true) closeApp = true;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            closeApp = true;
            this.Close();
        }

        private void btnMinimize_Click(object sender, EventArgs e)
        {
            if (GMusicBox.Core.Setting.StandardSetting.MinimizeToTray)
                fm.nfyMain_DoubleClick(null, null);
            else this.WindowState = FormWindowState.Minimized;
        }

        private void tVolume_Scroll(object sender, EventArgs e)
        {
            if (tVolume.Tag == null || (tVolume.Tag != null && (double)tVolume.Tag != tVolume.Value))
            {
                tp.Show((int)(tVolume.Value * 100) == 0 ? "静音" : Math.Round(tVolume.Value * 100).ToString() + "%", tVolume, (int)(tVolume.Value * (tVolume.Width - 14) + 20), -2);
                tVolume.Tag = tVolume.Value;
                tVolume.Refresh();
            }
        }

        private void tVolume_ScrollEnd(object sender, EventArgs e)
        {
            tp.Hide(tVolume);
        }

        private void tVolume_ValueChanged(object sender, EventArgs e)
        {
            if (tVolume.AccessibleName != "")
            {
                tVolume.AccessibleName = "";
                return;
            }

            fm.be.Volume = tVolume.Value;
            fm.tVolume.AccessibleName = " ";
            fm.tVolume.Value = tVolume.Value;
            fm.RefreshMuteState();
        }

        //protected override void OnLocationChanged(EventArgs e)
        //{
        //    System.Diagnostics.Debug.Print("LocationChanged");
        //    base.OnLocationChanged(e);
        //}
    }
}
